import Vue from "vue";
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import { Toast } from 'mint-ui'
import App from "./App.vue";
import router from "./router";
import axios from 'axios'
import store from "./store";
import 'amfe-flexible'

Vue.use(MintUI);
Vue.config.productionTip = false;
axios.defaults.headers.common['Authentication-Token'] = store.state.token;
// 环境的切换
if (process.env.NODE_ENV == 'development') {
  axios.defaults.baseURL = '/api';
} else if (process.env.NODE_ENV == 'debug') {
  axios.defaults.baseURL = '';
} else if (process.env.NODE_ENV == 'production') {
  axios.defaults.baseURL = 'http://api.123dailu.com/';
}
// 请求超时时间
axios.defaults.timeout = 10000;
// 添加请求拦截器  
axios.interceptors.request.use(config => {
  // 在发送请求之前做些什么  
  //判断是否存在token，如果存在将每个页面header都添加token  
  if (store.state.token) {
    config.headers.common['Authentication-Token'] = store.state.token
  }
  return config;
}, error => {
  // 对请求错误做些什么
  return Promise.reject(error);
});
// http response 拦截器  
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          this.$store.commit('del_token');
          router.replace({
            path: '/',
            query: { redirect: router.currentRoute.fullPath }//登录成功后跳入浏览的当前页面  
          })
      }
    }
    return Promise.reject(error.response.data)
  });
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
